﻿<template>
  <div
    class="app-container"
    v-loading="uploadLoading"
    element-loading-text="设备导入中,请稍后"
    element-loading-background="rgba(255,255,255,1)"
  >
    <div class="filter-title">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('upload.fileName')+' :'">
          <el-input
            v-model="listQuery.name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('upload.name')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('upload.createTime')+' :'">
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.startCreateTime"
              :picker-options="pickerOptionsB"
              type="date"
              :placeholder="$t('upload.startCreateTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.endCreateTime"
              :picker-options="pickerOptionsE"
              type="date"
              :placeholder="$t('upload.endCreateTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t('table.search') }}</el-button>
        <el-button
          class="filter-item"
          style="margin-left: 10px"
          type="primary"
          @click="handleRefresh"
        >{{ $t('table.refresh') }}</el-button>
        <el-button
          style="margin-left: 10px;float:right;position: relative;"
          class="filter-item"
          type="primary"
          icon="el-icon-edit"
        >
          <input
            ref="referenceUpload"
            class="posale"
            type="file"
            accept="excel/*"
            @change="(e)=>{return changeUpload(e,'uploadLoading')}"
          />上传EXCEL
        </el-button>
        <el-button
          style="margin-left: 10px;float:right"
          class="filter-item"
          type="primary"
          icon="el-icon-edit"
        >
          <a
            href="https://aiadvertise.oss-cn-hangzhou.aliyuncs.com/%E4%B8%8A%E4%BC%A0%E6%A8%A1%E6%9D%BF.xlsx"
          >下载EXCEL模板</a>
        </el-button>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255,255,255,0.2)"
      border
      fit
      highlight-current-row
      style="width: 100%min-height:1000px"
    >
      <el-table-column fixed="left" align="center" :label="$t('table.id')" width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('upload.fileName')">
        <template slot-scope="scope">
          <span>
            <a class="linkName" :href="scope.row.rource_url">{{ scope.row.file_name }}</a>
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('upload.successCount')">
        <template slot-scope="scope">
          <span>{{ scope.row.success_count }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('upload.errorCount')">
        <template slot-scope="scope">
          <span>{{ scope.row.error_count }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('upload.errorUrl')">
        <template slot-scope="scope">
          <span>
            <a class="linkName" :href="scope.row.error_url">{{ scope.row.error_name }}</a>
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('upload.createTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import selectPage from "@/views/select/SelectPage"; // 分页选择控件
import Cookies from "js-cookie";

export default {
  name: "EquipmentAppalication",
  components: {
    "select-page": selectPage
  },
  data() {
    return {
      uploadLoading: false,
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      regionType: null,
      regionId: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: "",
        startCreateTime: "",
        endCreateTime: ""
      },
      merchantNumber: 1,
      merchantSearch: "",
      permission: {
        del: false,
        update: false,
        review: false
      },
      pickerOptionsB: {
        // 限制时间
        // selectableRange: ['22:00:00 - 23:59:59','00:00:00 - 02:00:00'],
        // 限制日期
        disabledDate: this.disabledDateB
      },
      pickerOptionsE: {
        disabledDate: this.disabledDateE
      }
    };
  },
  computed: {
    operWidth: function() {
      let itemCount = 1;
      if (this.permission.del) {
        itemCount += 1;
      }
      if (this.permission.update) {
        itemCount += 1;
      }
      if (this.permission.review) {
        itemCount += 1;
      }
      return itemCount * 80 + "px";
    }
  },
  created() {
    if (this.$store.state.permission.jurisdiction === "*") {
      this.permission.del = true;
      this.permission.update = true;
      this.permission.review = true;
    } else {
      const jurisdiction = this.$store.state.permission.jurisdiction.split(",");
      jurisdiction.forEach(element => {
        const id = parseInt(element);
        if (id === 142) {
          this.permission.del = true;
        } else if (id === 140) {
          this.permission.update = true;
        } else if (id === 141) {
          this.permission.review = true;
        }
      });
    }
    this.regionId = this.$store.state.user.id;
    this.regionType = this.$store.state.user.regionType;
    this.getList();
  },
  methods: {
    disabledDateB(time) {
      if (this.listQuery.endTime) {
        return (
          time.getTime() >
          new Date(this.listQuery.endTime).getTime() + 24 * 3600 * 1000 - 8.64e7
        );
      }
    },
    disabledDateE(time) {
      if (this.listQuery.startTime) {
        return (
          time.getTime() < new Date(this.listQuery.startTime).getTime() - 8.64e6
        );
      }
    },
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page
      };
      if (this.listQuery.name) {
        data["name"] = this.listQuery.name;
      }
      if (this.listQuery.startCreateTime) {
        data["startCreateTime"] = this.listQuery.startCreateTime;
      }
      if (this.listQuery.endCreateTime) {
        const date = new Date(this.listQuery.endCreateTime);
        data["endCreateTime"] = this.timestampToTime(
          date.getTime() + (60 * 60 * 24 * 1000 - 1)
        );
      }
      const equipmentRegisterPath = {
        // 获取批量导入列表
        company: "STQ",
        api: "pc_upload_index",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(equipmentRegisterPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        name: "",
        startCreateTime: "",
        endCreateTime: ""
      };
      this.getList();
    },
    // excel导入
    changeUpload(element, loading) {
      this[loading] = true;
      const fileName = element.target.files[0].name;
      let fileext = fileName.substring(
        fileName.lastIndexOf("."),
        fileName.length
      );
      fileext = fileext.toLowerCase();
      if (fileext === ".xlsx") {
        if (element.target.files[0].size <= 20 * 1024 * 1024) {
          const id = JSON.parse(Cookies.get("admin_token")).id;
          const file = new FormData();
          file.append("file", element.target.files[0]);
          file.append("admin_id", id);
          this.updateExcel(file, result => {
            if (result.data.status === 0) {
              this.$message.error(result.data.msg);
            } else {
              this.$notify({
                title: "成功",
                message: result.data.msg,
                type: "success",
                duration: 2000
              });
            }
            this.$refs.referenceUpload.value = null;
            this.uploadLoading = false;
            this.getList();
          });
        } else {
          this.$message.error("文件不得超过20MB");
          this[loading] = false;
          return false;
        }
      } else {
        this.$message.error("请上传xlsx文件");
        this[loading] = false;
        return false;
      }
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "pc_merchant_getName") {
            this.merchantOptions = resj.data;
            this.merchantCount = resj.count;
          } else if (resj.api === "pc_equipment_checkEquipment") {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: "成功",
              message: "更新成功",
              type: "success",
              duration: 2000
            });
          } else if (resj.api === "pc_upload_index") {
            this.total = resj.count;
            this.list = resj.data;
            this.listLoading = false;
          } else if (resj.api === "pc_equipment_delete") {
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        }
      }
    },
    equipmentTypeEs() {
      console.log("123", this.typeOptions);
    }
  }
};
</script>
